package com.yiren.dbaa.module.exam.api.impl;

import android.app.Activity;
import android.util.SparseArray;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.db.dao.cache.ProjectCache;
import com.yiren.backstage.judge.api.IExamJudge;
import com.yiren.dbaa.constant.PointCode;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.module.exam.api.IExamProjectApi;
import com.yiren.dbaa.module.exam.view.dialog.ExamSelectDialog;
import com.yiren.dbaa.module.exam.view.dialog.LightChooseDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/19
 */
public class ExamProjectApiImpl implements IExamProjectApi {

    private Activity activity;
    private UserParamConfigBean userParamConfig;


    public ExamProjectApiImpl(Activity activity) {
        this.activity = activity;
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public List<ProjectEntity> getProjectList() {
        List<ProjectEntity> projects = new ArrayList<>();
        // 增加灯光模拟项目（默认）
        ProjectEntity point = new ProjectEntity();
        point.setId("41700l");
        point.setName(StringConstant.LIGHT_SIMULATION);
        point.setProjectType(ProjectTypeEnum.LIGHT.toString());
        projects.add(point);
        if (null != ProjectCache.subjectThreeDayProjects) {
            SparseArray<ProjectEntity> subjectThreeDayProjects = ProjectCache.subjectThreeDayProjects;
            for (int i = 0; i < subjectThreeDayProjects.size(); i++) {
                ProjectEntity projectEntity = subjectThreeDayProjects.get(subjectThreeDayProjects.keyAt(i));
                projects.add(projectEntity);
            }
        }
        return projects;
    }

    @Override
    public List<ProjectEntity> getLightProjects() {
        List<ProjectEntity> projects = new ArrayList<>();
        if (null != ProjectCache.lightProjects) {
            SparseArray<ProjectEntity> lightProjects = ProjectCache.lightProjects;
            for (int i = 0; i < lightProjects.size(); i++) {
                projects.add(lightProjects.get(lightProjects.keyAt(i)));
            }
        }
        if (projects.size() > 0) {
            ProjectEntity projectEntity = new ProjectEntity();
            projectEntity.setId("0l");
            projectEntity.setName("随机灯光");
            projects.add(0, projectEntity);
        }
        return projects;
    }

    @Override
    public ProjectEntity getKbtcProject(List<ProjectEntity> projects) {
        for (ProjectEntity projectEntity : projects) {
            if (PointCode.KBTC_CODE.equals(String.valueOf(projectEntity.getId()))) {
                return projectEntity;
            }
        }
        return null;
    }

    @Override
    public List<String> getKbtcReqProjects() {
        List<String> kbtcProjects = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L8.toString());
        if (null == kbtcProjects) {
            kbtcProjects = new ArrayList<>();
        }
        return kbtcProjects;
    }

    @Override
    public void setProjectState(List<ProjectEntity> projects, ProjectEntity projectEntity, TaskStateEnum taskState) {
        for (int i = 0; i < projects.size(); i++) {
            if (projectEntity.getId().equals(projects.get(i).getId())) {
                projects.get(i).setTaskState(taskState);
                break;
            }
        }
    }

    @Override
    public ProjectEntity loadStartPointInfo(int startExamProject, List<ProjectEntity> lightProjects) {
        ProjectEntity project;
        if (lightProjects.size() == 0 && startExamProject == 2) {
            startExamProject = 1;  //如果没有灯光列表，则默认选择上车准备
        }
        if (startExamProject == 2) { //灯光项目
            project = lightProjects.get(new Random().nextInt(lightProjects.size() - 1) + 1);
        } else {
            String projectId;
            if (startExamProject == 1) projectId = PointCode.SCZB_CODE;
            else projectId = PointCode.QIBU_CODE;
            project = ProjectCache.getPointById(projectId);
            if (project == null) project = new ProjectEntity(projectId);
        }
        return project;
    }

    @Override
    public ProjectEntity loadFollowProject(ProjectEntity projectEntity, TaskStateEnum taskState, List<ProjectEntity> lightProjects) {
        ProjectEntity project = null;
        if (PointCode.SCZB_CODE.equals(projectEntity.getId()) && taskState == TaskStateEnum.FINISH) {
            //上车准备执行完成，则获取上车准备后接项目
            String sczbNextProject = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L2.toString());
            if (!sczbNextProject.equals("0")) {
                if (PointCode.YJXS_CODE.equals(sczbNextProject)) {  //如果是随机灯光
                    if (lightProjects.size() > 0) {
                        project = lightProjects.get(new Random().nextInt(lightProjects.size() - 1) + 1);
                    }
                } else {
                    project = ProjectCache.getPointById(sczbNextProject);
                }
            }
        } else if (projectEntity.getProjectType().equals(ProjectTypeEnum.LIGHT.toString()) && taskState == TaskStateEnum.FINISH) {
            //灯光执行完成，则获取灯光后接项目
            String mndgNextProject = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L3.toString());
            if (!mndgNextProject.equals("0")) {
                project = ProjectCache.getPointById(mndgNextProject);
            }
        }
        return project;
    }

    @Override
    public ProjectEntity loadFullJudgeProject() {
        return ProjectCache.subjectThreeFullJudge;
    }

    @Override
    public boolean isLockedProjectEntity(ProjectEntity projectEntity, List<ProjectEntity> executedPoint) {
        List<String> pointPlaySettings = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L10.toString());
        if (null != pointPlaySettings && pointPlaySettings.size() > 0) {
            for (ProjectEntity project : executedPoint) {
                //如果已经执行项目中存在当前项目，再判断该项目是否只执行一次
                if (projectEntity.getId() == project.getId()) {
                    if (pointPlaySettings.contains(projectEntity.getId())) {
                        //如果在“点位只执行一次”集合中存在改点位，则锁定该点位
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean isLockedKbtcProject(float trainingMileage, List<String> kbtcProjects) {
        boolean notReachedMileageNoKbtc = (boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L6.toString());
        if (notReachedMileageNoKbtc && trainingMileage < 1000f * (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L4.toString())) { //未达到里程，不能靠边停车
            MessageBox.show(activity, "未达到里程，不能靠边停车！");
            return true;
        }
        boolean notCompleteProjectNoKbtc = (boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L7.toString());
        if (notCompleteProjectNoKbtc) {  //未完成项目，不能靠边停车
            if (kbtcProjects.size() > 0) {
                MessageBox.show(activity, "考试项目没完成前，不能靠边停车！");
                return true;
            }
        }
        return false;
    }

    @Override
    public void startLightProject(List<ProjectEntity> lightProjects, IExamJudge iExamJudge) {
        if (lightProjects.size() == 0) {
            MessageBox.show(activity, "灯光列表为空！");
        } else {
            ExamSelectDialog.getLightSelectDialog(activity, lightProjects).setOnLightItemClickListener(new LightChooseDialog.OnLightItemClickListener() {
                @Override
                public void onLightItemClick(int position) {
                    if (position == 0) { //随机灯光
                        iExamJudge.startProjectJudge(lightProjects.get(new Random().nextInt(lightProjects.size() - 1) + 1));
                    } else {
                        iExamJudge.startProjectJudge(lightProjects.get(position));
                    }
                }
            });
        }
    }

    @Override
    public boolean filterCurProject(ProjectEntity curExecuteProject, ProjectEntity executeProject) {
        if (curExecuteProject != null && curExecuteProject.getId() == executeProject.getId()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean autoExecuteKbtc(ProjectEntity curExecuteProject, List<String> kbtcProjects, float trainingMileage) {
        if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L5.toString()) && trainingMileage > (1000f * (float) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L4.toString()))) { //达到里程自动执行靠边停车
            if (curExecuteProject != null && curExecuteProject.getName().contains("靠边"))  //如果当前正在执行靠边停车项目，则不自动触发靠边停车了
                return false;
            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L7.toString()) && kbtcProjects.size() > 0) {
                return false;
            }
            return true;
        }
        return false;
    }
}
